<?php

class M_tkel extends CI_Model{
	
	function M_tkel(){
		parent::__construct();
	}
	
	function get_list(){
		$query = "SELECT VALIDTO, NOURUT, GRADE, KODEJAB, NIK, RPTISTRI, RPTANAK, USERNAME
		FROM tkeluarga";
			
		$result = $this->db->query($query);
		$nbrows = $result->num_rows();
	
		$jsonresult = "";
		$data = array();
		if($nbrows>0){
			$i=0;
			foreach($result->result() as $row){
				$data[$i] = $row;
				$i++;
			}
			return "{\"data\":" .json_encode($data). "}";
		} else {
			return "{\"data\":" .$jsonresult. "}";
		}
	}
	
	function create($validto,$nourut,$grade,$kodejab,$nik,$rptistri,$rptanak,$username){
		$sql = "SELECT * FROM tkeluarga WHERE NIK='".$nik."'";
		$rs = $this->db->query($sql);
		if($rs->num_rows()>0){
			return 0;
		}else{
			$data = array(
					"VALIDTO"=>$validto,
					"NOURUT"=>$nourut,
					"GRADE"=>$grade,
					"KODEJAB"=>$kodejab,
					"NIK"=>$nik,
					"RPTISTRI"=>$rptistri,
					"RPTANAK"=>$rptanak,
					"USERNAME"=>$username
			);
			$this->db->insert('tkeluarga', $data);
			if($this->db->affected_rows()){
				return 1;
			}else{
				return 0;
			}
		}
	}
	
	function update($validto,$nourut,$grade,$kodejab,$nik,$rptistri,$rptanak,$username){
		$data = array(
					"VALIDTO"=>$validto,
					"NOURUT"=>$nourut,
					"GRADE"=>$grade,
					"KODEJAB"=>$kodejab,
					"NIK"=>$nik,
					"RPTISTRI"=>$rptistri,
					"RPTANAK"=>$rptanak,
					"USERNAME"=>$username
			);
		$this->db->where('NIK', $nik);
		$this->db->update('tkeluarga', $data);
		
		return 1;
	}
	
	function delete($grade){
		$this->db->where('GRADE', $grade);
		$this->db->delete('tkeluarga');
		
		return 1;
	}
	
}

?>